﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Diagnostics;
using System.Collections.Generic;

namespace WidgetLibrary
{
    /// <summary>
    /// Show the Isolated Storage struct for your application.
    /// </summary>
    public static class IsoViewer
    {
		/// <summary>
		/// Show file system from Isolated Storage
		/// </summary>
		/// <param name="pattern">Use * to show all files types</param>
		/// <param name="storeFile">IsolatedStorageFile to show</param>
        public static void GetIsolatedStorageView(string pattern, IsolatedStorageFile storeFile)
        {
            
            string root = System.IO.Path.GetDirectoryName(pattern);

            if (root != "")
                root += "/";

            string[] directories = storeFile.GetDirectoryNames(pattern);
			//if the root directory has not FOLDERS, then the GetFiles() method won't be called.
			//the line below calls the GetFiles() method in this event so files are displayed
			//even if there are no folders
			if (directories.Length == 0) 
                GetFiles(root, "*", storeFile); 


            for (int i = 0; i < directories.Length; i++)
            {
                string dir = directories[i] + "/";                

                //Write to output window
                Debug.WriteLine(root + directories[i]);

                //Get all the files from this directory
                GetFiles(root + directories[i], pattern, storeFile);

                //Continue to get the next directory
                GetIsolatedStorageView(root + dir + "*", storeFile);

            }

            
        }


        /// <summary>
        /// Print files from dir in Isolated Storage
        /// </summary>
        /// <param name="dir">Folder to show files</param>
        /// <param name="pattern">Use * to show all files types</param>
        /// <param name="storeFile">IsolatedStorageFile to show</param>
        private static void GetFiles(string dir, string pattern, IsolatedStorageFile storeFile)
        {

            string fileString = System.IO.Path.GetFileName(pattern);
            string[] files = storeFile.GetFileNames(pattern);

            try
            {
                for (int i = 0; i < storeFile.GetFileNames(dir + "/" + fileString).Length; i++)
                {
                    //Files are prefixed with "--" 
                    Debug.WriteLine("--" + dir + "/" + storeFile.GetFileNames(dir + "/" + fileString)[i]);
                }
            }
            catch (IsolatedStorageException ise)
            {
                Debug.WriteLine("An IsolatedStorageException exception has occurred: " + ise.InnerException);
            }
            catch (Exception e)
            {
                Debug.WriteLine("An exception has occurred: " + e.InnerException);
            }



        } 



    }
}
